sap.ui.define([
    "sap/ui/core/mvc/Controller",
    'sap/ui/model/json/JSONModel'
],
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, JSONModel) {
        "use strict";

        return Controller.extend("c080.controller.Main", {
            onInit: function () {
                this._wizard = this.byId("CreateProductWizard");
                this._oNavContainer = this.byId("wizardNavContainer");
                this._oWizardContentPage = this.byId("wizardContentPage");

                this.model = new JSONModel();
                this.model.setData({
                    productNameState: "Error",
                    productUnitState: "Error",
                    productName: "测试用物料010",
                    productUnit: "EA",
                    productGroup: "20",
                    productDescription: "SAP（Systems, Applications, and Products）是一套全球领先的企业管理软件解决方案，帮助企业整合各业务流程，提高效率。其包括ERP（企业资源规划）、CRM（客户关系管理）、SCM（供应链管理）等模块，涵盖财务、人力资源、生产等方面。SAP通过实时数据处理和集成，优化企业运营，提供智能决策支持。其灵活性和可扩展性使其适用于各行各业，成为全球众多企业的首选解决方案，助力业务创新和数字化转型。",
                    optionalDescription: "UI5是SAP开发的用户界面框架，基于HTML5技术，用于构建现代、响应式、直观的企业级Web应用程序。它支持模块化、扩展性强，提供丰富的控件库，简化开发流程，实现优秀的用户体验。",
                    productType: "成品",
                    productTypeIcon: "sap-icon://database",
                    inStock: false,
                    outStock: false,
                    complete: false
                });
                this.getView().setModel(this.model);

                
            },
            onAfterRendering: function () {
                var stepA = this.byId("A")
                var stepB = this.byId("B")
                stepA.setNextStep(stepB)    
            },
            ChangeStep:function(){
                
                var wizard2 = this.byId("customStep")
                wizard2.discardProgress(wizard2.getSteps()[0]);
                var stepA = this.byId("A")
                var stepC = this.byId("C")

                stepA.setNextStep(stepC)    
                
                
            },
            additionalInfoValidation: function () {
                var name = this.byId("ProductName").getValue();
                var unit = this.byId("ProductUnit").getValue();

                if (unit != "EA") {
                    this._wizard.setCurrentStep(this.byId("ProductInfoStep"));
                    this.model.setProperty("/productUnitState", "Error");
                } else {
                    this.model.setProperty("/productUnitState", "None");
                }

                if (name.length < 6) {
                    this._wizard.setCurrentStep(this.byId("ProductInfoStep"));
                    this.model.setProperty("/productNameState", "Error");
                } else {
                    this.model.setProperty("/productNameState", "None");
                }

                if (name.length < 6 || unit != "EA") {
                    this._wizard.invalidateStep(this.byId("ProductInfoStep"));
                } else {
                    this._wizard.validateStep(this.byId("ProductInfoStep"));
                }
            },
            setProductTypeFromSegmented: function (evt) {

                var productType = evt.getParameters().item.getText();
                var productTypeIcon = evt.getParameters().item.getIcon();

                this.model.setProperty("/productType", productType);
                this.model.setProperty("/productTypeIcon", productTypeIcon);

            },
            onCheckBoxSelect: function (evt) {
                // debugger
                var oText = evt.getSource().getProperty('text');
                var isSelected = evt.getParameters().selected;
                if (oText === "内部制作") {
                    this.model.setProperty("/inStock", isSelected);
                } else if (oText === "外部采购") {
                    this.model.setProperty("/outStock", isSelected);
                }


            },
            wizardCompletedHandler: function () {
                this._oNavContainer.to(this.byId("wizardReviewPage"));
            },
            inputComplete: function () {
                this.model.setProperty("/complete", true)
               
            },
            goToPage: function (pageno) {
                var fnAfterNavigate = function () {
                    this._wizard.goToStep(this._wizard.getSteps()[pageno]);
                    this._oNavContainer.detachAfterNavigate(fnAfterNavigate);
                }.bind(this);

                this._oNavContainer.attachAfterNavigate(fnAfterNavigate);
                this._oNavContainer.backToPage(this._oWizardContentPage.getId());
            },
            editStep1: function () {
                this.goToPage(0)
            },
            editStep2: function () {
                this.goToPage(1)
            },
            editStep3: function () {
                this.goToPage(2)
            },
            handleWizardCancel: function () {
                // alert(11)
                this._oNavContainer.backToPage(this._oWizardContentPage.getId());
                this._wizard.discardProgress(this._wizard.getSteps()[0]);
            }

        });
    });
